<template>
  <div class="container-table">
    <el-table
      :data="goodsList"
      style="width: 100%"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column fixed prop="goods_id" label="流水号" />
      <el-table-column prop="goods_code" label="条码" />
      <el-table-column prop="goods_name" label="商品名称" />
      <el-table-column prop="goods_unit" label="单位" />
      <el-table-column prop="goods_price" label="当前售价" />
      <el-table-column prop="goods_reserve" label="库存" />
      <el-table-column prop="onetypes[0].category_oneName" label="一级类别" />
      <el-table-column prop="twotypes[0].category_twoName" label="二级类别" />
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button
            link
            type="primary"
            size="small"
            @click="handleMod(scope.row)"
            >编辑</el-button
          >
          <el-button
            link
            type="danger"
            size="small"
            @click="handleDel(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="container-pagination">
      <el-pagination
        v-model:currentPage="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[1, 2, 3, 4, 5, 6, 7]"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
    <GoodsModify ref="goodsModify"></GoodsModify>
  </div>
</template>

<script setup>
import { computed, getCurrentInstance, toRaw } from 'vue'
import { useStore } from 'vuex'
import { reqUpdateGoods } from '@/api/goods'
import { ElMessage, ElMessageBox } from 'element-plus'
import GoodsModify from './GoodsModify'

const poxy = getCurrentInstance()
const store = useStore()
const { dispatch, commit, getters } = store

// 编辑按钮
const handleMod = (goods) => {
  commit('goods/SAVEGOODS', goods)
  poxy.refs.goodsModify.changeDialogFormVisible()
}
// 删除按钮
const handleDel = async (goods) => {
  // 确认删除
  ElMessageBox.confirm('确定删除该商品?', '提醒', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      // 删除商品
      const result = await reqUpdateGoods({
        goods_id: goods.goods_id,
        goods_status: 0
      })
      if (result.code === 200 && result.data === 1) {
        getGoodsList()
        ElMessage({
          message: '删除成功！',
          type: 'success'
        })
      } else {
        ElMessage.error('删除失败！')
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除'
      })
    })
}
// 每页数量改变的方法
const handleSizeChange = (val) => {
  pagination.value.pageSize = val
  getGoodsList()
}
// 页数改变的方法
const handleCurrentChange = (val) => {
  pagination.value.currentPage = val
  getGoodsList()
}
// 获取商品列表
const getGoodsList = () => {
  dispatch('goods/getGoodsList', {
    data: toRaw(goodsSearchParams.value),
    pagination
  })
}
const goodsList = computed(() => getters.goodsList)
const pagination = computed(() => getters.goodsPagination)
const goodsSearchParams = computed(() => getters.goodsSearchParams)

getGoodsList()
</script>

<style lang="scss" scoped></style>
